<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="楼宇ID" prop="buildingId"
                :rules="[
                  {required: true, message:'楼宇ID不能为空', trigger:'blur'}
                 ]">
                <el-select v-model="inputForm.buildingId" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in buildingNameList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="客户姓名" prop="customerName"
                :rules="[
                  {required: true, message:'客户姓名不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.customerName" placeholder="请填写客户姓名"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="联系电话" prop="phone"
                :rules="[
                  {required: true, message:'联系电话不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.phone" placeholder="请填写联系电话"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="楼宇地址" prop="buildingAddress"
                :rules="[
                  {required: true, message:'楼宇地址不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.buildingAddress" placeholder="请填写楼宇地址"     ></el-input>
           </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col> -->
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          buildingId: '',
          customerName: '',
          phone: '',
          buildingAddress: '',
          remarks: ''
        },
        buildingNameList: []
      }
    },
    created () {
      this.getAllBuildingName()
    },
    components: {
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建客户管理列表`
        } else if (method === 'edit') {
          this.title = '修改客户管理列表'
        } else if (method === 'view') {
          this.title = '查看客户管理列表'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/tcustomerinfo/tCustomerInfo/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.tCustomerInfo)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/tcustomerinfo/tCustomerInfo/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      },
      // 获取所有楼宇
      getAllBuildingName () {
        this.$http({
          url: '/tcustomerinfo/tCustomerInfo/getAllBuildingName',
          method: 'get'
        }).then(({data}) => {
          if (data && data.success) {
            console.log(data)
            data.page.forEach(element => {
              this.buildingNameList.push({
                value: element.id,
                label: element.buildingName
              })
            });
          }
        })
      },
    }
  }
</script>